package example.POI;

import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;
import java.util.Base64;
import java.util.List;


public class POITest {



    public static void main(String[] args) {

        String filePath = "/Users/zgw/temp/poitest.xlsx";
        try (FileInputStream fis = new FileInputStream(filePath);
             XSSFWorkbook workbook = new XSSFWorkbook(fis)) {

            XSSFSheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
            List<XSSFShape> shapes = sheet.createDrawingPatriarch().getShapes();

            for (XSSFShape shape : shapes) {
                if (shape instanceof XSSFPicture) {
                    XSSFPicture picture = (XSSFPicture) shape;
                    XSSFPictureData pictureData = picture.getPictureData();
                    ClientAnchor anchor = picture.getClientAnchor();

                    System.out.println("图片类型: " + pictureData.getPictureType());
                    System.out.println("图片格式: " + pictureData.suggestFileExtension());
                    System.out.println("图片位置 - Row1: " + anchor.getRow1() + " Col1: " + anchor.getCol1());

                    byte[] bytes = pictureData.getData();
                    String base64 = Base64.getEncoder().encodeToString(bytes);
//                    System.out.println("data:image/png;base64," + base64);
                    // 此处可以处理图片数据，例如保存到文件系统等

                    POIImages.getInstance().addImage(new POIImageItem(anchor.getRow1(), anchor.getCol1(), base64));
                }
                else {
                    System.out.println("shape: " + shape);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("POIImages.getInstance().getImages().size() = " + POIImages.getInstance().getImages().size());
//        for (POIImageItem item : POIImages.getInstance().getImages()) {
//            System.out.println("row: " + item.row + " col: " + item.col + " base64: " + item.base64);
//        }
    }
}
